<?php

namespace App\Http\Controllers\Manage;

use App\Http\Model\Website\WebsiteArticleModel;
use App\Http\Model\Website\WebsiteBannerModel;
use App\Http\Model\Website\WebsiteCategoryModel;
use App\Http\Model\Website\WebsiteNavigationModel;
use App\Http\Model\Website\WebsiteTemplateModel;
use Illuminate\Http\Request;
use Illuminate\View\View;

class WebsiteController extends BasicController
{
    //
    public function pageTableTemplateView(Request $request): View
    {
        return view($this->viewPath . 'website.template');
    }

    public function pageTableTemplateData(Request $request): array|string
    {
        $data = $request->all();
        $page = $data['page'] ? $data['page'] -1 : 0;
        $limit = $data['limit'] ?? 10;
        // 需要处理查询的相关条件
        $where = [
            [ 'deleted', "=", 0],
        ];
        $pageData = WebsiteTemplateModel::where($where)->orderBy('id','desc')->offset($page * $limit)->limit($limit)->get()->toarray();
        $count = WebsiteTemplateModel::where($where)->count();
        return successReturn("加载成功", [
            "data" => $pageData,
            "count" => $count
        ]);
    }

    public function deleteInfoArticleAction(Request $request, string $code): array|string
    {
        return $this->modelDelete(new WebsiteArticleModel, $code, $request);
    }

    public function statusInfoArticleAction(Request $request, string $code): array|string
    {
        return $this->modelStatus(new WebsiteArticleModel, $code, $request);
    }

    public  function editInfoArticleAction(Request $request): array|string
    {
        $data = $request->all();
        $has = WebsiteCategoryModel::where([
            ['code', '=', $data["code"]],
        ])->first();
        if (!$has)
        {
            return errorReturn("导航不存在");
        }
        $loginInfo = $this->getLoginInfo($request);
        $admin = $loginInfo['admin'];
        $role = $loginInfo['role'];
        $update = (new WebsiteCategoryModel)->updateInfo(dataToWhere([
            "code"=>$data["code"],
            "version"=>$has->version,
        ]), [
            "ltwc001"=>$data["ltwc001"],
            "ltwc002"=>$data["ltwc002"],
            "ltwc003"=>$data["ltwc003"],
            "pCode"=>$data["pCode"],
            "uLtaCode"=>$admin->code,
            "uLtarCode"=>$role->code,
        ]);
        if ($update)
        {
            return successReturn("操作成功");
        }
        return errorReturn("操作失败");
    }
    public function addInfoArticleAction(Request $request): array|string
    {
        $data = $request->all();
        $loginInfo = $this->getLoginInfo($request);
        $admin = $loginInfo['admin'];
        $role = $loginInfo['role'];
        $adminId = (new WebsiteCategoryModel)->createInfo([
            "ltwa001"=>$data["ltwa001"],
            "ltwa002"=>$data["ltwa002"],
            "ltwa003"=>$data["ltwa003"],
            "ltwa004"=>$data["ltwa004"],
            "ltwa005"=>$data["ltwa005"],
            "ltwa006"=>$data["ltwa006"],
            "ltwcCode"=>$data["ltwcCode"],
            "cLtaCode"=>$admin->code,
            "uLtaCode"=>$admin->code,
            "uLtarCode"=>$role->code,
        ]);
        if ($adminId > 0 )
        {
            return successReturn("添加成功");
        }
        else
        {
            return errorReturn("添加失败");
        }
    }


    public function pageTableArticleView(Request $request): View
    {
        $category = (new WebsiteCategoryModel)->getArray();
        return view($this->viewPath . 'website.article', ["categoryList"=>$category]);
    }

    public function pageTableArticleData(Request $request): array|string
    {
        $data = $request->all();
        $page = $data['page'] ? $data['page'] -1 : 0;
        $limit = $data['limit'] ?? 10;
        // 需要处理查询的相关条件
        $where = [
            [ 'deleted', "=", 0],
        ];
        $pageData = WebsiteArticleModel::where($where)->orderBy('id','desc')->offset($page * $limit)->limit($limit)->get()->toarray();
        $count = WebsiteArticleModel::where($where)->count();
        return successReturn("加载成功", [
            "data" => $pageData,
            "count" => $count
        ]);
    }

    public function deleteInfoCategoryAction(Request $request, string $code): array|string
    {
        return $this->modelDelete(new WebsiteCategoryModel, $code, $request);
    }

    public function statusInfoCategoryAction(Request $request, string $code): array|string
    {
        return $this->modelStatus(new WebsiteCategoryModel, $code, $request);
    }

    public  function editInfoCategoryAction(Request $request): array|string
    {
        $data = $request->all();
        $has = WebsiteCategoryModel::where([
            ['code', '=', $data["code"]],
        ])->first();
        if (!$has)
        {
            return errorReturn("导航不存在");
        }
        $loginInfo = $this->getLoginInfo($request);
        $admin = $loginInfo['admin'];
        $role = $loginInfo['role'];
        $update = (new WebsiteCategoryModel)->updateInfo(dataToWhere([
            "code"=>$data["code"],
            "version"=>$has->version,
        ]), [
            "ltwc001"=>$data["ltwc001"],
            "ltwc002"=>$data["ltwc002"],
            "ltwc003"=>$data["ltwc003"],
            "pCode"=>$data["pCode"],
            "uLtaCode"=>$admin->code,
            "uLtarCode"=>$role->code,
        ]);
        if ($update)
        {
            return successReturn("操作成功");
        }
        return errorReturn("操作失败");
    }
    public function addInfoCategoryAction(Request $request): array|string
    {
        $data = $request->all();
        $loginInfo = $this->getLoginInfo($request);
        $admin = $loginInfo['admin'];
        $role = $loginInfo['role'];
        $adminId = (new WebsiteCategoryModel)->createInfo([
            "ltwc001"=>$data["ltwc001"],
            "ltwc002"=>$data["ltwc002"],
            "ltwc003"=>$data["ltwc003"],
            "pCode"=>$data["pCode"],
            "cLtaCode"=>$admin->code,
            "uLtaCode"=>$admin->code,
            "uLtarCode"=>$role->code,
        ]);
        if ($adminId > 0 )
        {
            return successReturn("添加成功");
        }
        else
        {
            return errorReturn("添加失败");
        }
    }

    public function pageTableCategoryView(Request $request): View
    {
        $category = (new WebsiteCategoryModel)->getArray();
        return view($this->viewPath . 'website.category', ["categoryList"=>$category]);
    }

    public function pageTableCategoryData(Request $request): array|string
    {
        $where = [
            [ 'deleted', "=", 0],
        ];
        $pageData = WebsiteCategoryModel::where($where)->orderBy('id','desc')->get()->toarray();
        $treeData = dataToTree($pageData, "code", "pCode");
        return successReturn("加载成功", $treeData);
    }

    public function deleteInfoBannerAction(Request $request, string $code): array|string
    {
        return $this->modelDelete(new WebsiteBannerModel, $code, $request);
    }

    public function statusInfoBannerAction(Request $request, string $code): array|string
    {
        return $this->modelStatus(new WebsiteBannerModel, $code, $request);
    }

    public  function editInfoBannerAction(Request $request): array|string
    {
        $data = $request->all();
        $has = WebsiteBannerModel::where([
            ['code', '=', $data["code"]],
        ])->first();
        if (!$has)
        {
            return errorReturn("导航不存在");
        }
        $loginInfo = $this->getLoginInfo($request);
        $admin = $loginInfo['admin'];
        $role = $loginInfo['role'];
        $update = (new WebsiteBannerModel)->updateInfo(dataToWhere([
            "code"=>$data["code"],
            "version"=>$has->version,
        ]), [
            "ltwb001"=>$data["ltwb001"],
            "ltwb002"=>$data["ltwb002"],
            "ltwb003"=>$data["ltwb003"],
            "ltwnCode"=>$data["ltwnCode"],
            "uLtaCode"=>$admin->code,
            "uLtarCode"=>$role->code,
        ]);
        if ($update)
        {
            return successReturn("操作成功");
        }
        return errorReturn("操作失败");
    }
    public function addInfoBannerAction(Request $request): array|string
    {
        $data = $request->all();
        $loginInfo = $this->getLoginInfo($request);
        $admin = $loginInfo['admin'];
        $role = $loginInfo['role'];
        $adminId = (new WebsiteBannerModel)->createInfo([
            "ltwb001"=>$data["ltwb001"],
            "ltwb002"=>$data["ltwb002"],
            "ltwb003"=>$data["ltwb003"],
            "ltwnCode"=>$data["ltwnCode"],
            "cLtaCode"=>$admin->code,
            "uLtaCode"=>$admin->code,
            "uLtarCode"=>$role->code,
        ]);
        if ($adminId > 0 )
        {
            return successReturn("添加成功");
        }
        else
        {
            return errorReturn("添加失败");
        }
    }

    public function pageTableBannerView(Request $request): View
    {
        $nagvigations = (new WebsiteNavigationModel)->getArray();
        return view($this->viewPath . 'website.banner',  ["navigationList"=>$nagvigations]);
    }

    public function pageTableBannerData(Request $request): array|string
    {
        $data = $request->all();
        $page = $data['page'] ? $data['page'] -1 : 0;
        $limit = $data['limit'] ?? 10;
        // 需要处理查询的相关条件
        $where = [
            [ 'deleted', "=", 0],
        ];
        $pageData = WebsiteBannerModel::where($where)->orderBy('id','desc')->offset($page * $limit)->limit($limit)->get()->toarray();
        $count = WebsiteBannerModel::where($where)->count();
        return successReturn("加载成功", [
            "data" => $pageData,
            "count" => $count
        ]);
    }

    public function deleteInfoNavigationAction(Request $request, string $code): array|string
    {
        $has = WebsiteNavigationModel::where([
            ['code', '=', $code],
        ])->first();
        if (!$has)
        {
            return errorReturn("导航不存在");
        }
        $loginInfo = $this->getLoginInfo($request);
        $admin = $loginInfo['admin'];
        $role = $loginInfo['role'];
        $underThisNavigation = $this->getAllNavigationUnderCode($code);
        $delete = (new WebsiteNavigationModel)->deleteInfo([], [
            "uLtaCode"=>$admin->code,
            "uLtarCode"=>$role->code,
        ], "code", $underThisNavigation);
        if ($delete)
        {
            return successReturn("操作成功");
        }
        return errorReturn("操作失败");
    }

    public function statusInfoNavigationAction(Request $request, string $code): array|string
    {
        $has = WebsiteNavigationModel::where([
            ['code', '=', $code],
        ])->first();
        if (!$has)
        {
            return errorReturn("导航不存在");
        }
        $loginInfo = $this->getLoginInfo($request);
        $admin = $loginInfo['admin'];
        $role = $loginInfo['role'];
        $update = (new WebsiteNavigationModel)->updateInfo(dataToWhere([
            "code"=>$code,
            "version"=>$has->version,
        ]), [
            "status"=>$has->status == 1 ? 0: 1,
            "uLtaCode"=>$admin->code,
            "uLtarCode"=>$role->code,
        ]);
        if ($update)
        {
            return successReturn("操作成功");
        }
        return errorReturn("操作失败");
    }

    public  function editInfoNavigationAction(Request $request): array|string
    {
        $data = $request->all();
        $has = WebsiteNavigationModel::where([
            ['code', '=', $data["code"]],
        ])->first();
        if (!$has)
        {
            return errorReturn("导航不存在");
        }
        $loginInfo = $this->getLoginInfo($request);
        $admin = $loginInfo['admin'];
        $role = $loginInfo['role'];
        $update = (new WebsiteNavigationModel)->updateInfo(dataToWhere([
            "code"=>$data["code"],
            "version"=>$has->version,
        ]), [
            "ltwn001"=>$data["ltwn001"],
            "ltwn002"=>$data["ltwn002"],
            "ltwn003"=>$data["ltwn003"],
            "pCode"=>$data["pCode"],
            "uLtaCode"=>$admin->code,
            "uLtarCode"=>$role->code,
        ]);
        if ($update)
        {
            return successReturn("操作成功");
        }
        return errorReturn("操作失败");
    }
    public function addInfoNavigationAction(Request $request): array|string
    {
        $data = $request->all();
        $loginInfo = $this->getLoginInfo($request);
        $admin = $loginInfo['admin'];
        $role = $loginInfo['role'];
        $adminId = (new WebsiteNavigationModel)->createInfo([
            "ltwn001"=>$data["ltwn001"],
            "ltwn002"=>$data["ltwn002"],
            "ltwn003"=>$data["ltwn003"],
            "pCode"=>$data["pCode"],
            "cLtaCode"=>$admin->code,
            "uLtaCode"=>$admin->code,
            "uLtarCode"=>$role->code,
        ]);
        if ($adminId > 0 )
        {
            return successReturn("添加成功");
        }
        else
        {
            return errorReturn("添加失败");
        }
    }

    public function pageTableNavigationView(Request $request): View
    {
        $nagvigations = (new WebsiteNavigationModel)->getArray();
        return view($this->viewPath . 'website.navigation', ["navigationList"=>$nagvigations]);
    }

    public function pageTableNavigationData(Request $request): array|string
    {
        // 需要处理查询的相关条件
        $where = [
            [ 'deleted', "=", 0],
        ];
        $pageData = WebsiteNavigationModel::where($where)->orderBy('id','desc')->get()->toarray();
        $treeData = dataToTree($pageData, "code", "pCode");
        return successReturn("加载成功", $treeData);
    }
}
